package com.hnao.serialport.utils.sqym;


import android.util.Log;

public class CardReaderHelper {
    private static final String TAG = CardReaderHelper.class.getSimpleName();
    private CardReaderListener cardReaderListener;
    private SerialPortKit cardReaderKit;

    private CardReaderHelper() {
    }

    public static final CardReaderHelper getInstance() {
        return CardReaderHelper.SingleHolder.INSTANCE;
    }

    public void initCardReader(String path, int baudRate, int dataStartBit, int dataLength) {
        Log.e(TAG, "正在初始化刷卡模块");
        if (this.cardReaderKit != null) {
            Log.e(TAG, "刷卡模块不为空，不用初始化");
        } else {
            this.cardReaderKit = new SerialPortKit();
            this.cardReaderKit.setSpecialSerialPort();
            if (!this.cardReaderKit.openSerialPort(path, baudRate)) {
                this.cardReaderKit = null;
                throw new RuntimeException("串口开启失败 " + path + " " + baudRate);
            }

            this.cardReaderKit.setOnSerialPortChanged(new SerialPortKit.SerialPortListener() {
                public void onSerialPortChanged(byte[] buffer) {
                    String data = CardReaderHelper.bytesToHexString(buffer);
                    Log.i(CardReaderHelper.TAG, "解析后的卡值" + data);
                    if (CardReaderHelper.this.cardReaderListener != null) {
                        CardReaderHelper.this.cardReaderListener.onCardReaderListener(data);
                    }

                }
            }, dataStartBit, dataLength);
        }

    }

    public void colsePort() {
        if (this.cardReaderKit != null) {
            this.cardReaderKit.closeSerialPort();
            this.cardReaderKit = null;
        }

    }

    public void writeData(byte[] data) throws Exception {
        if (this.cardReaderKit == null) {
            throw new Exception("Please perform initCardReader()");
        } else {
            this.cardReaderKit.writeData(data);
        }
    }

    public void writeHexData(String data) throws Exception {
        if (this.cardReaderKit == null) {
            throw new Exception("Please perform initCardReader()");
        } else {
            this.cardReaderKit.writeData(SerialPortKit.hexStringToBytes(data));
        }
    }

    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src != null && src.length > 0) {
            for(int i = 0; i < src.length; ++i) {
                int v = src[i] & 255;
                String hv = Integer.toHexString(v);
                if (hv.length() < 2) {
                    stringBuilder.append("0");
                }

                stringBuilder.append(hv);
            }

            return stringBuilder.toString();
        } else {
            return null;
        }
    }

    public void setCardReaderListener(CardReaderListener cardReaderListener) {
        this.cardReaderListener = cardReaderListener;
    }

    private static class SingleHolder {
        private static final CardReaderHelper INSTANCE = new CardReaderHelper();

        private SingleHolder() {
        }
    }

    public interface CardReaderListener {
        void onCardReaderListener(String var1);
    }
}
